package com.insight.config.oss;

import lombok.extern.slf4j.Slf4j;
import com.insight.common.util.MinioUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Minio文件上传配置文件
 */
@Slf4j
@Configuration
@ConditionalOnProperty(name = "jeecg.minio.minio_url")
public class MinioConfig {
    @Value(value = "${jeecg.minio.minio_url}")
    private String minioUrl;
    @Value(value = "${jeecg.minio.externalNetwork}")
    private String externalNetworkUrl;
    @Value(value = "${jeecg.minio.minio_name}")
    private String minioName;
    @Value(value = "${jeecg.minio.minio_pass}")
    private String minioPass;
    @Value(value = "${jeecg.minio.bucketName}")
    private String bucketName;

    //    @Value(value = "${jeecg.minio.port}")
    private int port;

    @Bean
    public void initMinio() {
        //在这里最好解析出port来，因为最后的port是必须被用到的。正确的返回上传文件后minio的url
        if (port == 0) {
            int index = minioUrl.lastIndexOf(':');
            port = Integer.parseInt(minioUrl.substring(index + 1, minioUrl.endsWith("/") ? minioUrl.length() - 1 : minioUrl.length()));
            minioUrl = minioUrl.substring(0, index);
        }
        if (minioUrl.startsWith("https")) {
            MinioUtil.setSecure(true);
        }
        if (!minioUrl.startsWith("http")) {
            minioUrl = "http://" + minioUrl;
        }
        MinioUtil.setMinioUrl(minioUrl);
        MinioUtil.setExternalNetworkUrl(externalNetworkUrl);
        MinioUtil.setMinioName(minioName);
        MinioUtil.setMinioPass(minioPass);
        MinioUtil.setBucketName(bucketName);
        MinioUtil.setPort(port);
        MinioUtil.initMinio();
    }

}
